<template>
  <div class="game-error-page">
    <div class="game-container">
      <!-- 游戏角色 - 守卫/骑士 -->
      <div class="game-character">
        <div class="knight-helmet"></div>
        <div class="knight-face">
          <div class="eye left"></div>
          <div class="eye right"></div>
        </div>
        <div class="knight-body"></div>
        <div class="knight-arm left">
          <div class="shield"></div>
        </div>
        <div class="knight-arm right">
          <div class="sword"></div>
        </div>
        <div class="knight-leg left"></div>
        <div class="knight-leg right"></div>
      </div>

      <!-- 城堡大门 -->
      <div class="castle-gate">
        <div class="gate-top"></div>
        <div class="gate-door left"></div>
        <div class="gate-door right"></div>
        <div class="lock"></div>
        <div class="gate-sign">AUTHORIZED PLAYERS ONLY</div>
      </div>

      <!-- 文本内容 -->
      <div class="game-text">
        <h1 class="glitch-title" data-text="401">401</h1>
        <h2>哎呀！你没有通行证！</h2>
        <p>看起来你需要更高的权限等级才能进入这个区域。也许你需要先完成前面的任务？</p>

        <div class="game-stats">
          <div class="stat">
            <span class="label">权限等级</span>
            <div class="auth-bar">
              <div class="auth-fill"></div>
            </div>
          </div>
          <div class="stat">
            <span class="label">装备需求</span>
            <div class="req-bar">
              <div class="req-fill"></div>
              <div class="req-marker"></div>
            </div>
          </div>
        </div>

        <div class="inventory">
          <div class="inventory-title">你的物品栏</div>
          <div class="inventory-slots">
            <div class="slot empty"></div>
            <div class="slot">
              <div class="item potion"></div>
            </div>
            <div class="slot empty"></div>
            <div class="slot">
              <div class="item coin"></div>
            </div>
            <div class="slot empty key-slot">
              <div class="key-required">需要钥匙</div>
            </div>
          </div>
        </div>

        <div class="game-controls">
          <router-link class="game-btn continue" to="/login">
            <i class="fas fa-key"></i> 获取通行证
          </router-link>
          <router-link class="game-btn retreat" to="/">
            <i class="fas fa-home"></i> 返回主城
          </router-link>
          <button class="game-btn secret" @click="tryUnlock">
            <i class="fas fa-lock"></i> 尝试开锁
          </button>
        </div>

        <!-- 开锁小游戏 -->
        <div v-if="showLockpick" class="mini-game">
          <div class="game-instructions">
            旋转拨盘找到正确位置！<span class="attempts">尝试次数: {{ attempts }}/5</span>
          </div>
          <div class="lockpick-game">
            <div :style="{ transform: `rotate(${dialPosition}deg)` }" class="lock-dial">
              <div class="dial-mark"></div>
            </div>
            <div class="lock-target"></div>
            <div class="lock-controls">
              <button class="dial-btn left" @click="rotateDial(-15)">
                <i class="fas fa-arrow-left"></i>
              </button>
              <button class="dial-btn try" @click="checkLock">
                尝试
              </button>
              <button class="dial-btn right" @click="rotateDial(15)">
                <i class="fas fa-arrow-right"></i>
              </button>
            </div>
          </div>
          <div v-if="lockMessage" :class="{ success: lockSuccess }" class="lock-message">
            {{ lockMessage }}
          </div>
        </div>
      </div>

      <!-- 装饰元素 -->
      <div class="floating-particles"></div>
      <div class="torch left">
        <div class="flame"></div>
      </div>
      <div class="torch right">
        <div class="flame"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'GameUnauthorized',
  data() {
    return {
      showLockpick: false,
      dialPosition: 0,
      targetPosition: 0,
      attempts: 5,
      lockMessage: '',
      lockSuccess: false
    }
  },
  methods: {
    tryUnlock() {
      this.showLockpick = true;
      this.attempts = 5;
      this.lockMessage = '';
      this.lockSuccess = false;
      this.dialPosition = 0;
      // 生成随机目标位置 (0-360)
      this.targetPosition = Math.floor(Math.random() * 360);
    },
    rotateDial(amount) {
      this.dialPosition = (this.dialPosition + amount) % 360;
      if (this.dialPosition < 0) this.dialPosition += 360;
    },
    checkLock() {
      // 检查是否接近目标位置 (允许15度误差)
      const diff = Math.abs(this.dialPosition - this.targetPosition);
      const isClose = diff <= 15 || diff >= 345;

      this.attempts--;

      if (isClose) {
        this.lockSuccess = true;
        this.lockMessage = '成功！你解开了锁！';

        // 2秒后重定向到登录页面
        setTimeout(() => {
          this.$router.push('/login');
        }, 2000);
      } else if (this.attempts <= 0) {
        this.lockMessage = '失败！守卫发现了你！';

        // 2秒后关闭游戏
        setTimeout(() => {
          this.showLockpick = false;
        }, 2000);
      } else {
        // 给出提示
        if (diff < 180) {
          this.lockMessage = this.dialPosition < this.targetPosition ?
            '差一点！试着往右转更多...' :
            '差一点！试着往左转更多...';
        } else {
          this.lockMessage = this.dialPosition < this.targetPosition ?
            '差远了！试着往左转更多...' :
            '差远了！试着往右转更多...';
        }
      }
    }
  }
}
</script>

<style scoped>
.game-error-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #2c3e50 0%, #4a5568 100%);
  font-family: 'Press Start 2P', cursive, 'Arial', sans-serif;
  overflow: hidden;
  position: relative;
}

.game-container {
  position: relative;
  width: 90%;
  max-width: 800px;
  padding: 2rem;
  background-color: white;
  border-radius: 15px;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
  text-align: center;
  overflow: hidden;
  z-index: 1;
}

/* 骑士/守卫角色 */
.game-character {
  position: absolute;
  top: 20px;
  left: 30px;
  width: 80px;
  height: 120px;
  animation: guardMove 4s infinite;
}

.knight-helmet {
  position: absolute;
  top: 0;
  left: 20px;
  width: 40px;
  height: 25px;
  background-color: #7f8c8d;
  border-radius: 5px 5px 0 0;
  border: 3px solid #333;
}

.knight-face {
  position: absolute;
  top: 25px;
  left: 25px;
  width: 30px;
  height: 20px;
  background-color: #e0e0e0;
  border-radius: 0 0 10px 10px;
  border: 3px solid #333;
  border-top: none;
}

.eye {
  position: absolute;
  width: 5px;
  height: 5px;
  background-color: #333;
  border-radius: 50%;
  top: 8px;
}

.eye.left {
  left: 7px;
}

.eye.right {
  right: 7px;
}

.knight-body {
  position: absolute;
  top: 45px;
  left: 20px;
  width: 40px;
  height: 40px;
  background-color: #7f8c8d;
  border-radius: 5px;
  border: 3px solid #333;
}

.knight-arm {
  position: absolute;
  width: 10px;
  height: 30px;
  background-color: #7f8c8d;
  border: 2px solid #333;
  border-radius: 5px;
}

.knight-arm.left {
  top: 50px;
  left: 10px;
  transform: rotate(-20deg);
}

.knight-arm.right {
  top: 50px;
  right: 10px;
  transform: rotate(20deg);
}

.shield {
  position: absolute;
  width: 20px;
  height: 25px;
  background-color: #e74c3c;
  border: 2px solid #333;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  top: 15px;
  left: -15px;
  transform: rotate(20deg);
}

.sword {
  position: absolute;
  width: 5px;
  height: 40px;
  background-color: #bdc3c7;
  border: 1px solid #333;
  top: 15px;
  right: -5px;
  transform: rotate(-20deg);
}

.sword::before {
  content: '';
  position: absolute;
  width: 10px;
  height: 5px;
  background-color: #f39c12;
  border: 1px solid #333;
  bottom: -5px;
  left: -3px;
}

.knight-leg {
  position: absolute;
  width: 10px;
  height: 30px;
  background-color: #34495e;
  border: 2px solid #333;
  border-radius: 5px;
}

.knight-leg.left {
  bottom: 0;
  left: 25px;
}

.knight-leg.right {
  bottom: 0;
  right: 25px;
}

/* 城堡大门 */
.castle-gate {
  margin: 0 auto 30px;
  width: 200px;
  height: 150px;
  position: relative;
}

.gate-top {
  width: 100%;
  height: 30px;
  background-color: #7f8c8d;
  border-radius: 5px 5px 0 0;
  border: 3px solid #333;
  position: relative;
}

.gate-top::before,
.gate-top::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 40px;
  background-color: #7f8c8d;
  border: 3px solid #333;
  top: -30px;
}

.gate-top::before {
  left: 30px;
  border-radius: 5px 5px 0 0;
}

.gate-top::after {
  right: 30px;
  border-radius: 5px 5px 0 0;
}

.gate-door {
  position: absolute;
  width: 50%;
  height: 120px;
  background-color: #8c7b5e;
  border: 3px solid #333;
  bottom: 0;
}

.gate-door.left {
  left: 0;
  border-right: none;
  animation: doorShake 2s infinite;
}

.gate-door.right {
  right: 0;
  border-left: none;
  animation: doorShake 2s infinite reverse;
}

.lock {
  position: absolute;
  width: 40px;
  height: 40px;
  background-color: #f1c40f;
  border: 3px solid #333;
  border-radius: 50%;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1;
}

.lock::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 15px;
  background-color: #e74c3c;
  border-radius: 5px;
  top: 12px;
  left: 10px;
}

.lock::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: #333;
  border-radius: 50%;
  bottom: 10px;
  left: 15px;
}

.gate-sign {
  position: absolute;
  width: 180px;
  padding: 5px;
  background-color: #e74c3c;
  color: white;
  font-size: 0.6rem;
  border: 2px solid #333;
  border-radius: 5px;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
}

/* 文本样式 */
.game-text {
  position: relative;
  z-index: 2;
}

.glitch-title {
  font-size: 5rem;
  margin: 0;
  color: #e74c3c;
  position: relative;
  display: inline-block;
}

.glitch-title::before,
.glitch-title::after {
  content: attr(data-text);
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.glitch-title::before {
  left: 2px;
  text-shadow: -1px 0 #f1c40f;
  clip: rect(44px, 450px, 56px, 0);
  animation: glitch-anim 5s infinite linear alternate-reverse;
}

.glitch-title::after {
  left: -2px;
  text-shadow: -1px 0 #3498db;
  clip: rect(44px, 450px, 56px, 0);
  animation: glitch-anim2 5s infinite linear alternate-reverse;
}

h2 {
  font-size: 1.5rem;
  margin-top: 0.5rem;
  margin-bottom: 1rem;
  color: #333;
}

p {
  font-size: 1rem;
  line-height: 1.6;
  margin-bottom: 2rem;
  color: #666;
}

/* 游戏状态栏 */
.game-stats {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 200px;
}

.label {
  font-size: 0.8rem;
  margin-bottom: 5px;
  color: #333;
}

.auth-bar, .req-bar {
  width: 100%;
  height: 15px;
  background-color: #e0e0e0;
  border-radius: 10px;
  overflow: hidden;
  border: 2px solid #333;
  position: relative;
}

.auth-fill {
  width: 20%;
  height: 100%;
  background-color: #e74c3c;
  animation: pulse 2s infinite;
}

.req-fill {
  width: 60%;
  height: 100%;
  background-color: #3498db;
}

.req-marker {
  position: absolute;
  width: 3px;
  height: 20px;
  background-color: #e74c3c;
  top: -2px;
  left: 80%;
}

/* 物品栏 */
.inventory {
  margin-bottom: 20px;
  border: 3px solid #333;
  border-radius: 8px;
  padding: 10px;
  background-color: #f0f0f0;
}

.inventory-title {
  font-size: 0.9rem;
  margin-bottom: 10px;
  color: #333;
}

.inventory-slots {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.slot {
  width: 50px;
  height: 50px;
  background-color: #e0e0e0;
  border: 2px solid #333;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.slot.empty {
  background-image: linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc),
  linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc);
  background-size: 10px 10px;
  background-position: 0 0, 5px 5px;
}

.key-slot {
  animation: pulse-border 2s infinite;
}

.key-required {
  position: absolute;
  font-size: 0.5rem;
  color: #e74c3c;
  bottom: -20px;
  width: 100%;
}

.item {
  width: 30px;
  height: 30px;
  border-radius: 5px;
}

.potion {
  background-color: #3498db;
  border: 2px solid #333;
  border-radius: 5px;
  position: relative;
}

.potion::before {
  content: '';
  position: absolute;
  width: 10px;
  height: 5px;
  background-color: #333;
  top: -5px;
  left: 8px;
}

.coin {
  background-color: #f1c40f;
  border: 2px solid #333;
  border-radius: 50%;
  position: relative;
}

.coin::before {
  content: '$';
  position: absolute;
  font-size: 0.8rem;
  color: #333;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* 按钮样式 */
.game-controls {
  display: flex;
  justify-content: center;
  gap: 15px;
  flex-wrap: wrap;
}

.game-btn {
  padding: 0.8rem 1.5rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  border: 3px solid #333;
  box-shadow: 0 4px 0 #333;
  position: relative;
  top: 0;
}

.game-btn:hover {
  top: -3px;
  box-shadow: 0 7px 0 #333;
}

.game-btn:active {
  top: 4px;
  box-shadow: 0 0 0 #333;
}

.continue {
  background: #f1c40f;
  color: #333;
}

.retreat {
  background: #3498db;
  color: white;
}

.secret {
  background: #e74c3c;
  color: white;
}

/* 开锁小游戏 */
.mini-game {
  margin-top: 30px;
  border: 3px solid #333;
  border-radius: 10px;
  padding: 15px;
  background-color: #f0f0f0;
}

.game-instructions {
  margin-bottom: 10px;
  font-size: 0.9rem;
  display: flex;
  justify-content: space-between;
}

.attempts {
  color: #e74c3c;
  font-weight: bold;
}

.lockpick-game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
}

.lock-dial {
  width: 100px;
  height: 100px;
  background-color: #7f8c8d;
  border-radius: 50%;
  border: 3px solid #333;
  position: relative;
  transition: transform 0.3s ease;
}

.dial-mark {
  position: absolute;
  width: 5px;
  height: 20px;
  background-color: #e74c3c;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}

.lock-target {
  width: 10px;
  height: 30px;
  background-color: #333;
  position: relative;
}

.lock-target::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 5px;
  background-color: #333;
  top: 0;
  left: -5px;
}

.lock-controls {
  display: flex;
  gap: 10px;
}

.dial-btn {
  padding: 8px 15px;
  border-radius: 5px;
  background-color: #7f8c8d;
  color: white;
  border: 2px solid #333;
  cursor: pointer;
  transition: all 0.2s;
}

.dial-btn:hover {
  background-color: #95a5a6;
}

.dial-btn.try {
  background-color: #f1c40f;
  color: #333;
}

.lock-message {
  margin-top: 15px;
  padding: 10px;
  background-color: #e74c3c;
  color: white;
  border-radius: 5px;
  animation: fadeIn 0.3s;
}

.lock-message.success {
  background-color: #2ecc71;
}

/* 装饰元素 */
.floating-particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.floating-particles::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(#e74c3c 1px, transparent 1px),
  radial-gradient(#f1c40f 1px, transparent 1px),
  radial-gradient(#3498db 1px, transparent 1px);
  background-size: 20px 20px;
  background-position: 0 0, 10px 10px, 15px 15px;
  animation: particleFloat 20s linear infinite;
}

.torch {
  position: absolute;
  width: 20px;
  height: 60px;
  background-color: #8c7b5e;
  border: 2px solid #333;
  border-radius: 5px;
}

.torch.left {
  top: 20px;
  left: 20px;
}

.torch.right {
  top: 20px;
  right: 20px;
}

.flame {
  position: absolute;
  width: 30px;
  height: 30px;
  background: #f39c12;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  top: -20px;
  left: -5px;
  animation: flicker 0.5s infinite alternate;
}

.flame::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background: #f1c40f;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  top: 5px;
  left: 5px;
}

.flame::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: white;
  border-radius: 50%;
  top: 10px;
  left: 10px;
}

/* 动画定义 */
@keyframes guardMove {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  25% {
    transform: translateY(-5px) rotate(-2deg);
  }
  75% {
    transform: translateY(-5px) rotate(2deg);
  }
}

@keyframes doorShake {
  0%, 100% {
    transform: translateX(0);
  }
  10%, 30%, 50%, 70%, 90% {
    transform: translateX(-2px);
  }
  20%, 40%, 60%, 80% {
    transform: translateX(2px);
  }
}

@keyframes glitch-anim {
  0% {
    clip: rect(31px, 9999px, 94px, 0);
  }
  10% {
    clip: rect(112px, 9999px, 76px, 0);
  }
  20% {
    clip: rect(85px, 9999px, 77px, 0);
  }
  30% {
    clip: rect(27px, 9999px, 97px, 0);
  }
  40% {
    clip: rect(64px, 9999px, 98px, 0);
  }
  50% {
    clip: rect(61px, 9999px, 85px, 0);
  }
  60% {
    clip: rect(99px, 9999px, 114px, 0);
  }
  70% {
    clip: rect(34px, 9999px, 115px, 0);
  }
  80% {
    clip: rect(98px, 9999px, 129px, 0);
  }
  90% {
    clip: rect(43px, 9999px, 96px, 0);
  }
  100% {
    clip: rect(82px, 9999px, 64px, 0);
  }
}

@keyframes glitch-anim2 {
  0% {
    clip: rect(125px, 9999px, 162px, 0);
  }
  10% {
    clip: rect(120px, 9999px, 147px, 0);
  }
  20% {
    clip: rect(113px, 9999px, 171px, 0);
  }
  30% {
    clip: rect(123px, 9999px, 153px, 0);
  }
  40% {
    clip: rect(117px, 9999px, 176px, 0);
  }
  50% {
    clip: rect(124px, 9999px, 151px, 0);
  }
  60% {
    clip: rect(121px, 9999px, 155px, 0);
  }
  70% {
    clip: rect(129px, 9999px, 175px, 0);
  }
  80% {
    clip: rect(116px, 9999px, 158px, 0);
  }
  90% {
    clip: rect(128px, 9999px, 142px, 0);
  }
  100% {
    clip: rect(125px, 9999px, 163px, 0);
  }
}

@keyframes pulse {
  0%, 100% {
    width: 20%;
  }
  50% {
    width: 25%;
  }
}

@keyframes pulse-border {
  0%, 100% {
    border-color: #333;
  }
  50% {
    border-color: #e74c3c;
  }
}

@keyframes particleFloat {
  0% {
    background-position: 0 0, 10px 10px, 15px 15px;
  }
  100% {
    background-position: 100px 100px, 110px 110px, 115px 115px;
  }
}

@keyframes flicker {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(0.95);
    opacity: 0.9;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .game-character,
  .torch {
    display: none;
  }

  .glitch-title {
    font-size: 3rem;
  }

  h2 {
    font-size: 1.2rem;
  }

  .game-stats {
    flex-direction: column;
    align-items: center;
  }

  .inventory-slots {
    flex-wrap: wrap;
  }

  .game-controls {
    flex-direction: column;
    align-items: center;
  }

  .game-btn {
    width: 100%;
    justify-content: center;
  }
}

/* 添加像素字体 */
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
</style>
